<template>
	<view>
		<dialog-shell title="用户登录" confirm-text="登录" ref="login" @confirm="confirmLogin">
			<image src="/static/icons/icon.png" mode="aspectFit" class="icon-logo"></image>
			<text class="info-welcome">WELCOME！</text>
			<text class="info-register" @click="clickRegister">没有账号？请注册</text>
			<view class="login-container">

				<input type="text" placeholder="请输入用户名" :value="userName" @input="inputGetuser"
					placeholder-class="input-holder" />
				<text class="iconfont icon-wode ic-user "></text>
			</view>
			<view class="login-container">
				<input :type="isPwd ? 'password' : 'text'" placeholder="请输入密码" :value="password"
					@input="inputGetpas" placeholder-class="input-holder" />
				<text :class="isPwd?'iconfont icon-mimayincang1 ic-pwd-status':'iconfont icon-mimayincang ic-pwd-status'" @click="switchPwd"></text>
				<text class="iconfont icon-yaochi ic-user"></text>
			</view>
		</dialog-shell>
	</view>
</template>

<script>
	export default {
		name: "login-dialog",
		data() {
			return {
				userName: '',
				password: '',
				showPassword: false,
				isPwd:true
			};
		},
		methods: {
			clickRegister(){
				uni.navigateTo({
					url: '/pages/register/register',
					success: res => {
						this.$refs.login.hideDialog()
					},
					fail: () => {},
					complete: () => {}
				});
			},
			confirmLogin() {
				if (this.userName.length == 0 || this.password.length == 0) {
					uni.showToast({
						title: "请输入用户名和密码",
						icon: "none"
					})
					return
				}
				let url = this.$params.host + this.$params.action_login
				let data = {
					username: this.userName,
					password: this.password
				}
				uni.showLoading({
					title: '登录中',
					mask: false
				});
				this.$req.post(url, data, res => {
					if (!res.data.success) {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						});
						this.$refs.login.show()
						return
					}
					let app=getApp()
					app.globalData.id=res.data.data.id
					app.globalData.token=res.data.data.token
					app.globalData.avatar=res.data.data.avatar
					app.globalData.nickName=res.data.data.nickName
					app.globalData.userName=res.data.data.userName
					app.globalData.email=res.data.data.email
					app.globalData.type=res.data.data.type
					this.$noti.post(this.$params.noti_login_status)
					// console.log(app.globalData);
					uni.showToast({
						title: res.data.message,
						icon:"none"
					});
					this.$emit("login")
				}, () => {
					uni.hideLoading()
				})
			},
			inputGetuser(e) {
				this.userName = e.detail.value
			},
			inputGetpas(e) {
				this.password = e.detail.value
			},
			show() {
				this.$refs.login.showDialog();
			},
			hide() {
				this.$refs.login.hideDialog();
			},
			switchPwd(){
				this.isPwd=!this.isPwd
			}
		}
	}
</script>

<style>
	@import url("./login-dialog.css");
</style>